package com.sgb.businessmodule.viewmodel;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.harisucici.jetpackbase.base.HBaseViewModel;
import com.sgb.businessmodule.BusinessConfig;
import com.sgb.businessmodule.entity.BusinessRequestEntity;
import com.sgb.businessmodule.entity.HomeBusinessListEntity;
import com.sgb.businessmodule.entity.PickFilterEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.countmenu.CountMenuEntity;
import java.util.List;

public class BusinessOpportunityHallModel extends HBaseViewModel {

    //大厅顶部菜单栏数据
    public MutableLiveData<List<CountMenuEntity>> countMenuList = new MutableLiveData();
    //大厅列表数据
    public MutableLiveData<List<HomeBusinessListEntity>> businessList = new MutableLiveData();
    //是否最后分页
    public boolean isLastPage;
    //商机类型列表数据
    public MutableLiveData<List<PickFilterEntity>> businessStyleList = new MutableLiveData();
    //商机分类四级联动数据
    public MutableLiveData<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>>> businessTypeList = new MutableLiveData();
    //商机分类二级联动数据
    public MutableLiveData<List<PickFilterEntity<List<PickFilterEntity>>>> businessType2List = new MutableLiveData();


    //省市二级列表联动数据
    public MutableLiveData<List<PickFilterEntity<List<PickFilterEntity>>>> addressList = new MutableLiveData();

    public BusinessOpportunityHallModel(@NonNull Application application) {
        super(application);
    }

    /*
     * 获取商机大厅顶部菜单栏数据
     * */
    public void requestBusinessMenuData() {
        BusinessConfig.getInstance().getApi().getMenuData().enqueue(new ICallBack<BaseEntity<List<CountMenuEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CountMenuEntity>> data) {
                List<CountMenuEntity> list = data.getData();
                if (list.size() > 0) {
                    list.remove(data.getData().size() - 1);
                }
                countMenuList.postValue(list);
            }
        });
    }

    /*
     * 获取商机大厅列表数据
     * */
    public void requestBusinessListData(BusinessRequestEntity entity) {
        BusinessConfig.getInstance().getApi().getHomeBusinessData(entity).enqueue(new ICallBack<BaseEntity<ListResponse<List<HomeBusinessListEntity>>>>(true) {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<HomeBusinessListEntity>>> data) {
                if (data.getData().getList() != null) {
                    isLastPage = data.getData().getCurrentPage() >= data.getData().getTotal();
                    businessList.postValue(data.getData().getList());
                }
            }
        });
    }

    /*
     * 获取商机类型列表数据
     * */
    public void requestBusinessStyleListData(String no) {
        BusinessConfig.getInstance().getApi().getBusinessStyle(no).enqueue(new ICallBack<BaseEntity<List<PickFilterEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickFilterEntity>> data) {
                businessStyleList.postValue(data.getData());
            }
        });
    }

    /*
     * 获取省市列表联动数据
     * */
    public void getCityList() {
        BusinessConfig.getInstance().getApi().getTwoCityList().enqueue(new ICallBack<BaseEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickFilterEntity<List<PickFilterEntity>>>> data) {
                addressList.postValue(data.getData());
            }
        });
    }

    /*
     * 获取商机分类四级联动数据
     * */
    public void requestBusinessTypeData() {
        BusinessConfig.getInstance().getApi().getBusinessTypeData("1").enqueue(new ICallBack<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>>> data) {
                businessTypeList.postValue(data.getData());
            }
        });
    }

    /*
     * 获取商机分类二级联动数据
     * */
    public void requestBusinessType2Data() {
        BusinessConfig.getInstance().getApi().getBusinessType2Data("1").enqueue(new ICallBack<BaseEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickFilterEntity<List<PickFilterEntity>>>> data) {
                businessType2List.postValue(data.getData());
            }
        });
    }

    /**
     * 存储历史搜索记录
     */
    public void setShardHistorySearch(String content) {
        StringBuilder builder = new StringBuilder();
        SharedPreferenceUtil util = SharedPreferenceUtil.getInstance();
        String hsValue = util.getSaveStringData(SharedPreferenceUtil.HISTORY_SEARCH_BUSINESS, "");
        if (!TextUtils.isEmpty(hsValue)) {
            if (!hsValue.contains(content)) {
                builder.append(hsValue).append("#").append(content);
            } else {
                builder.append(hsValue);
            }
        } else {
            builder.append(content);
        }
        util.saveData(SharedPreferenceUtil.HISTORY_SEARCH_BUSINESS, builder.toString());
    }

}
